package v1

import (
	"github.com/gin-gonic/gin"
	"manager/bll"
	"manager/model"
	"manager/server/web/middleware"
	"manager/utils"
)

var EbikeType = &ebikeType{}

func init() {
	// 注册路由
	RegisterRouter(EbikeType)
}

type ebikeType struct{}

func (a *ebikeType) Init(r *gin.RouterGroup) {
	g := r.Group("/device_type", middleware.Auth())
	{
		g.POST("/create", a.create)
		g.POST("/update", a.update)
		g.POST("/list", a.list)
		g.POST("/delete", a.delete)
		g.POST("/parent_node", a.parentNode)
		g.POST("/info", a.info)
		g.POST("/toggle_status", a.toggleStatus)
	}
}

func (a *ebikeType) create(c *gin.Context) {
	var (
		in  = &model.EbikeTypeCreateRequest{}
		err error
		id  int64
	)

	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}

	id, err = bll.EbikeType.Create(c.Request.Context(), in)
	if err != nil {
		c.Error(err)
		return
	}
	utils.ResponseOk(c, &model.EbikeTypeCreateResponse{
		ID: id,
	})
}

func (a *ebikeType) update(c *gin.Context) {
	var (
		in  = &model.EbikeTypeUpdateRequest{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}

	if err = bll.EbikeType.Update(c.Request.Context(), in); err != nil {
		c.Error(err)
		return
	}
	utils.ResponseOk(c, nil)
}

func (a *ebikeType) list(c *gin.Context) {
	var (
		in  = &model.EbikeTypeListRequest{}
		out = &model.EbikesTypeListResponse{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}

	if out, err = bll.EbikeType.List(c.Request.Context(), in); err != nil {
		c.Error(err)
		return
	}
	utils.ResponseOk(c, out)
}

func (a *ebikeType) delete(c *gin.Context) {
	var (
		in  = &model.EbikeTypeDeleteRequest{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}

	if err = bll.EbikeType.Delete(c.Request.Context(), in); err != nil {
		c.Error(err)
		return
	}
	utils.ResponseOk(c, nil)
}

// parentNode 查询父级车型
func (a *ebikeType) parentNode(c *gin.Context) {
	var (
		in  = &model.ParentEbikeTypeRequest{}
		out []*model.ParentEbikeTypeList
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}

	if out, err = bll.EbikeType.ParentList(c.Request.Context(), in); err != nil {
		c.Error(err)
		return
	}

	type Ret struct {
		Total int                          `json:"total"`
		List  []*model.ParentEbikeTypeList `json:"list"`
	}
	utils.ResponseOk(c, &Ret{
		Total: len(out),
		List:  out,
	})
}

func (a *ebikeType) info(c *gin.Context) {
	var (
		in  = &model.EbikeTypeBaseRequest{}
		out *model.EbikeTypeInfo
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}

	if out, err = bll.EbikeType.Info(c.Request.Context(), in); err != nil {
		c.Error(err)
		return
	}

	utils.ResponseOk(c, out)
}

func (a *ebikeType) toggleStatus(c *gin.Context) {
	var (
		in  = &model.EbikeTypeToggleStatusRequest{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}

	if err = bll.EbikeType.ToggleStatus(c.Request.Context(), in); err != nil {
		c.Error(err)
		return
	}

	utils.ResponseOk(c, nil)
}
